// pages/user/evaluate.js
//获取应用实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: '',
    goods: {}, //存储商品评论信息
    order_id: '', //订单id
    order_goods: {}, //订单商品
    files: [], //图片
    logistics_score: 0, //物流评分
    attitude_score: 0, //态度评分
    describe_score: 0, //描述评分
    describe_list: [{
      score: 1
    },
    {
      score: 2
    },
    {
      score: 3
    },
    {
      score: 4
    },
    {
      score: 5
    }
    ], //描述相符
    logistics_list: [{
      score: 1
    },
    {
      score: 2
    },
    {
      score: 3
    },
    {
      score: 4
    },
    {
      score: 5
    }
    ], //物流服务
    attitude_list: [{
      score: 1
    },
    {
      score: 2
    },
    {
      score: 3
    },
    {
      score: 4
    },
    {
      score: 5
    }
    ] //服务态度    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order_id = options.order_id || 32;
    var is_lottery = options.lottery || 1;

    this.getDetail(order_id, is_lottery);
    this.setData({
      order_id: order_id
    })
  },

  init(e) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 获取订单信息
   */
  getDetail: function (order_id, is_lottery) {
    var _this = this;
    wx.request({
      url: app.globalData.url + 'index.php?act=toutiao_member&op=evaluate&key=' + encodeURI(app.globalData.token),
      data: {
        order_id: order_id,
        is_lottery: is_lottery
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          if (res.data.datas.order_goods.length > 0) {
            var goods = _this.data.goods;
            for (var i = 0; i < res.data.datas.order_goods.length; i++) {

              var rec_id = res.data.datas.order_goods[i].goods_id;
              //订单商品默认选中五颗星
              res.data.datas.order_goods[i].score_list = [{
                score: 1,
                check: '_selected'
              },
              {
                score: 2,
                check: '_selected'
              },
              {
                score: 3,
                check: '_selected'
              },
              {
                score: 4,
                check: '_selected'
              },
              {
                score: 5,
                check: '_selected'
              }
              ];

              res.data.datas.order_goods[i].files = []; //存储订单商品用户上传的照片

              if (!goods[rec_id]) {
                goods[rec_id] = {};
              }

              goods[rec_id].comment = ''; //订单商品用户的评论

              goods[rec_id].evaluate_image = []; //存储上传到后台照片的名称

              goods[rec_id].score = 5; //默认评分5分
            }
          }
          _this.setData({
            order_goods: res.data.datas.order_goods,
            goods: goods
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.datas.error,
            showCancel: false,
            success: function (res) {
              wx.navigateBack({})
            }
          })
        }
      }
    })
  },

  /**
   * 提交
   */
  bindFormSubmit: function (e) {
    var _this = this;
    var order_id = _this.data.order_id;
    var store_deliverycredit = _this.data.logistics_score; //物流评分
    var store_servicecredit = _this.data.attitude_score; //态度评分
    var store_desccredit = _this.data.describe_score; //描述评分
    var msg = '';
    delete this.data.goods['undefined'];
    console.log(this.data.goods);
    if (store_desccredit == 0) {
      msg = '请给商品描述打分';
    }

    if (store_deliverycredit == 0 && msg == '') {
      msg = '请给物流服务打分'
    }

    if (store_servicecredit == 0 && msg == '') {
      msg = '请给服务态度打分';
    }

    if (msg != '') {
      tt.showToast({
        title: msg,
        icon: 'none'
      })
      return false;
    }

    var data = {};
    data.goods = JSON.stringify(this.data.goods); //数组转json字符串
    data.store_desccredit = store_desccredit;
    data.store_servicecredit = store_servicecredit;
    data.store_deliverycredit = store_deliverycredit;
    data.order_id = this.data.order_id;

    wx.request({
      url: app.globalData.url + 'index.php?act=toutiao_member&op=save&key=' + encodeURI(app.globalData.token),
      data: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '评价成功',
            icon: 'success',
            duration: 2000,
            success: function (req) {
              setTimeout(function () {
                tt.switchTab({
                  url:'/pages/my/my'
                })
              }, 2000)

            }
          })
        } else {

        }
      }
    })
  },
  /**
   * 上传图片到后台
   */
  uploadImage: function (file, key) {
    var _this = this;
    var goods = _this.data.goods;
    var order_goods = _this.data.order_goods;

    var rec_id = order_goods[key].goods_id;
    if (goods.length <= 0) {
      goods = {};
    }

    wx.uploadFile({
      url: app.globalData.url + 'index.php?act=member_evaluate&op=file_upload&key=' + encodeURI(app.globalData.token),
      filePath: file[0],
      name: 'file',
      success: function (res) {
        var data = JSON.parse(res.data); //字符串转json
        console.log(data);
        if (data.code == 200) {
          order_goods[key].files = order_goods[key].files.concat(file); //拼接数组
          if (!goods[rec_id]) {
            goods[rec_id] = {};
            goods[rec_id]['evaluate_image'] = [];
          }

          goods[rec_id]['evaluate_image'] = goods[rec_id]['evaluate_image'].concat(data.datas);
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          _this.setData({
            order_goods: order_goods,
            goods: goods
          })
        } else {

          return false;
        }
      }
    })
  },
  chooseImage: function (e) {
    var key = e.currentTarget.dataset.key;

    var that = this;

    var order_goods = that.data.order_goods;

    //判断照片数
    if (order_goods[key].files.length >= 5) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '最多上传5张图片',
        success: function (res) {

        }
      })
 
      return false;
    }

    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        that.uploadImage(res.tempFilePaths, key); //上传图片到后台
      }
    })
  },
  /**
   * 预览图片
   */
  previewImage: function (e) {
    var order_goods = this.data.order_goods;
    var key = e.currentTarget.dataset.key;

    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: order_goods[key].files // 需要预览的图片http链接列表
    })
  },
  /**
   * 描述相符
   */
  describe: function (e) {
    var score = e.currentTarget.id;
    var describe_list = this.data.describe_list;
    for (var i = 0; i < 5; i++) {
      if (i < score) {
        describe_list[i].check = '_selected';
      } else {
        describe_list[i].check = '';
      }
    }

    this.setData({
      describe_list: describe_list,
      describe_score: score
    })
  },
  /**
   * 物流服务
   */
  logistics: function (e) {
    var score = e.currentTarget.id;
    var logistics_list = this.data.logistics_list;
    for (var i = 0; i < 5; i++) {
      if (i < score) {
        logistics_list[i].check = '_selected';
      } else {
        logistics_list[i].check = '';
      }
    }

    this.setData({
      logistics_list: logistics_list,
      logistics_score: score
    })
  },
  /**
   * 服务态度
   */
  attitude: function (e) {
    var score = e.currentTarget.id;
    var attitude_list = this.data.attitude_list;
    for (var i = 0; i < 5; i++) {
      if (i < score) {
        attitude_list[i].check = '_selected';
      } else {
        attitude_list[i].check = '';
      }
    }

    this.setData({
      attitude_list: attitude_list,
      attitude_score: score
    })
  },
  /**
   * 商品评分
   */
  goodsScore: function (e) {
    var order_goods = this.data.order_goods;

    var key = e.currentTarget.dataset.key; //order_goods的键值

    var rec_id = this.data.order_goods[key].goods_id;

    var score = e.currentTarget.id; //评分

    for (var i = 0; i < 5; i++) {
      if (i < score) {
        order_goods[key].score_list[i].check = '_selected';
      } else {
        order_goods[key].score_list[i].check = '';
      }
    }

    var goods = this.data.goods;

    if (goods.length <= 0) {
      var goods = [];
    }

    if (!goods[rec_id]) {
      goods[rec_id] = {};
      goods[rec_id]['evaluate_image'] = [];
    }

    goods[rec_id].score = score;

    this.setData({
      order_goods: order_goods,
      goods: goods
    })

  },
  /**
   * 输入评价
   */
  bindText: function (e) {

    var key = this.data.key; //订单商品数组的键值
    var comment = e.detail.value; //评价内容
    var goods = this.data.goods;
    var order_goods = this.data.order_goods;
    var rec_id = order_goods[key].goods_id;

    if (goods.length <= 0) {
      var goods = {}
    }

    if (!goods[rec_id]) {
      goods[rec_id] = {};
      goods[rec_id]['evaluate_image'] = [];
    }

    goods[rec_id].comment = comment;

    this.setData({
      goods: goods
    })
  },
  /**
   * 选择输入的input
   */
  selectInput: function (e) {
    this.setData({
      key: e.currentTarget.dataset.key
    })
  }
})